package Day2025_04_12;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Demo05 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        int[] a = new int[n];
        int[] b = new int[m];
        for (int i = 0; i < n; i++) a[i] = in.nextInt();
        for (int i = 0; i < m; i++) b[i] = in.nextInt();
        Set<Long> set = new HashSet<>();
        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                long s = a[i] + b[j];
                if (s <= m + n && isNum(s)) {
                    if (!set.contains(s)) {
                        ret++;
                        set.add(s);
                    }
                }
            }
        }
        System.out.println(ret);
    }
    private static boolean isNum (long s) {
        if (s == 1 || s == 2) return false;
        for (long i = 2; i <= Math.pow(s, 0.5); i++) {
            if (s % i == 0) return false;
        }
        return true;
    }
}
